#!/bin/sh

#
# Test that applying signed firmware the streaming way works
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
file-resource TEST {
	host-path = "${TESTFILE_1K}"
}

task complete {
	on-resource TEST { raw_write(0) }
}
EOF

cat >$EXPECTED_META_CONF <<EOF
file-resource "TEST" {
  length=1024
  blake2b-256="b25c2dfe31707f5572d9a3670d0dcfe5d59ccb010e6aba3b81aad133eb5e378b"
}
task "complete" {
  on-resource "TEST" {
    funlist = {"2", "raw_write", "0"}
  }
}
EOF

# Create new keys
cd $WORK
$FWUP_CREATE -g
cd -

# Sign the firmware
$FWUP_CREATE -s $WORK/fwup-key.priv -c -f $CONFIG -o $FWFILE

# Check that the zip file was created as expected
check_meta_conf

# Check that applying the firmware without checking signatures works
cat $FWFILE | $FWUP_APPLY -q -a -d $IMGFILE -t complete

# Check that applying the firmware with checking signatures works
cat $FWFILE | $FWUP_APPLY -q -p $WORK/fwup-key.pub -a -d $IMGFILE -t complete
